﻿using System;
using System.Collections.Generic;
using System.Text;
using Graffiti.Core;
using DataBuddy;
using System.Data;
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace GraffitiExtras.Plugins.NewCommentNotifier
{
    public class NewCommentNotifier : GraffitiEvent
    {
        private const string _emailFormat = "<h3>New reply to your comment on {0} by {1}:</h3>{2}" +
                        "<p>[You can reply to this comment by navigating to <a href=\"{3}\">{3}</a>]</p>";

        public override string Name
        {
            get
            {
                return "Comment Reply Notifier";
            }
        }

        public override string Description
        {
            get
            {
                return
                    "Plug-in to send email notifications to commenters when someone replies " +
                    "to their comments. Done by <a href=\"http://nayyeri.net\">Keyvan Nayyeri</a>.";
            }
        }

        public override bool IsEditable
        {
            get
            {
                return false;
            }
        }

        public override void Init(GraffitiApplication ga)
        {
            ga.AfterInsert += new DataObjectEventHandler(ga_AfterInsert);
        }

        void ga_AfterInsert(DataBuddyBase dataObject, EventArgs e)
        {
            Comment comment = dataObject as Comment;

            if (comment != null)
            {
                try
                {
                    CommentCollection comments = CommentCollection.FetchByColumn
                        (Comment.Columns.PostId, comment.PostId);

                    List<string> receivers = new List<string>();

                    Regex regEx = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

                    foreach (Comment tempComment in comments)
                    {
                        if (comment.Body.ToLower().Contains("@" + tempComment.Name.ToLower()))
                        {
                            if ((!string.IsNullOrEmpty(tempComment.Email)) &&
                                (regEx.IsMatch(tempComment.Email)))
                            {
                                receivers.Add(tempComment.Email);
                            }
                        }
                    }

                    if (receivers.Count > 0)
                    {
                        MailMessage email = new MailMessage(SiteSettings.Get().EmailFrom,
                            string.Join(";", receivers.ToArray()));

                        email.Subject = string.Format("New Reply to Your Comment on {0}",
                            SiteSettings.Get().Title);

                        email.Body = string.Format(_emailFormat, comment.Post.Title, comment.Name,
                            comment.Body, new Macros().FullUrl(comment.Post.Url));

                        email.IsBodyHtml = true;
                        email.Priority = MailPriority.Normal;

                        if (Emailer.SendMailMessage(email))
                            Log.Info(this.Name + " Plug-In", "Email sent.");
                        else
                            Log.Error(this.Name + " Plug-In", "Email could not be sent.");
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(this.Name + " Plug-In", ex.Message + ex.StackTrace);
                }
            }
        }
    }
}
